#pragma once
#include <array>
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;

namespace TentamenVersion1
{
	class Statistics
	{
	private:
		array<int,1000001> Salary;
	public:

		Statistics()
		{
			this->clear();
		}

		void ReadSalaries(string fileName)
		{
			string strRow;
			ifstream fin;
			
			try
			{
				fin.open(fileName);
				clear();
				while (getline(fin, strRow))
				{
					istringstream iss(strRow);
					int a = 0;
					iss >> a;
					int value = Salary[a];
					value += 1;
					Salary[a] = value;
				}
				fin.close();
			}

			catch (exception e)
			{
				cerr << "Exception: " << fileName << endl;
				cerr << e.what() << endl;
			}
		}

		void clear()
		{
			for (int i = Salary.size() - 1; i >= 0; i--)
			{
				Salary[i] = 0;
			}
		}

		int calculate_mean()
		{
			int count = 0;
			double value = 0;

			for (int i = 0; i < Salary.size(); i++)
			{
				if (Salary[i] > 0)
				{
					if (i > 20000 && i < 1000000)
					{
						count += Salary[i];

						value += (Salary[i] * i);
					}
				}
			}

			return (value / count);
		}

		int calculate_median()
		{
			int countHalf = 0;
			int countToHalf = 0;

			for (int i = 0; i < Salary.size() - 1; i++)
			{
				if (Salary[i] > 0)
				{
					if (i > 20000 && i < 1000000)
					{
						countHalf += Salary[i];
					}
				}
			}

				countHalf = countHalf / 2;

			for (int i = 0; i < Salary.size() - 1; i++)
			{
				if (Salary[i] > 0)
				{
					if (i > 20000 && i < 1000000)
					{
						countToHalf += Salary[i];
					}
				}

				if (countHalf == countToHalf)
				{
					return i;
				}
			}
	
			
		}

		~Statistics()
		{
		}
	};
}

